﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TS_XY_Train_Repaire.UserCon.ucPub.Map;
using TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainList;
namespace TS_XY_Train_Repaire.Locomotive.TrainRepair
{
    public partial class Locomotive_Repair_Work_TrainIn_MainForm : DevExpress.XtraEditors.XtraForm
    {
        public static string _MapID = "-1";
        public static string _LoadType = "0";
        public static string _MapName = "";
        public DateTime _time = new DateTime();
        private Pub.ProgressBarForm _pbf = new Pub.ProgressBarForm();
        private delegate void funHandle(int nValue);
        private funHandle myHandle = null;
        public string _MWID, _BMWID, _BMWGuid, _TrainID, _TTID, _TrainName = "";
        public Locomotive_Repair_Work_TrainIn_MainForm()
        {
            InitializeComponent();
        }

        private void Locomotive_Repair_Work_TrainIn_MainForm_Load(object sender, EventArgs e)
        {
            _time = Convert.ToDateTime(TS_Win_Locomotive_MainForm.pUserInfoMain.LoginTime);
            _Initcb_TrainClass();
            if (cb_TrainClass.SelectedIndex > -1)
            {
                _Initcb_TrainRP();
                _Initcb_TrainType();
                if (cb_TrainType.SelectedIndex > -1)
                {
                    //未加入匹配的扣车计划和库内机车的排异
                    _Initcb_TrainNum();
                }
                if (cb_TrainRP.SelectedIndex > -1 && cb_TrainClass.SelectedIndex > -1 && cb_TrainType.SelectedIndex > -1)
                {
                    _Initcb_TrainMW();
                }
                _CreatWorkerCode();
            }

        }
        #region//--下拉列表数据加载--//
        private void _Initcb_TrainClass()
        {
            TS_Win_Class.FillListTypeAct(cb_TrainClass, "select f_id,f_name from TS_S_T_P_TrainClass where f_del=0");
        }
        private void _Initcb_TrainRP()
        {
            TS_Win_Class.FillListTypeAct(cb_TrainRP, "select f_id,f_name from TS_S_T_P_Train_RP where f_del=0 and f_id in (select f_rpid from TS_S_T_P_Train_RP_TC where f_del=0 and f_tcid=" + cb_TrainClass.SelectedValue.ToString() + " )");
        }
        private void _Initcb_TrainType()
        {
            TS_Win_Class.FillListTypeAct(cb_TrainType, "select f_id,f_name from TS_S_T_P_TrainType where f_del=0 and f_tcid=" + cb_TrainClass.SelectedValue.ToString());
        }
        private void _Initcb_TrainNum()
        {
            TS_Win_Class.FillListTypeAct(cb_TrainNum, "select f_id,f_name from TS_S_T_P_Train_List where f_del=0 and f_ttid=" + cb_TrainType.SelectedValue.ToString() + " and f_id not in (select f_trainid from TS_TR_BU_Work_Main where f_del=0  and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (9))) order by f_name asc");
        }
        private void _Initcb_TrainMW()
        {
            TS_Win_Class.FillListTypeAct(cb_TrainMW, "select f_id,f_name from TS_TR_PS_Work_Main where f_type=" + _LoadType + " and f_del=0 and f_funcid_2=" + cb_TrainType.SelectedValue.ToString() + " and f_funcid_1=" + cb_TrainClass.SelectedValue.ToString() + " and f_rpid=" + cb_TrainRP.SelectedValue.ToString());
        }
        #endregion
        #region//--生成作业号--//
        private void _CreatWorkerCode()
        {
            try
            {
                tb_WorkCode.Text = cb_TrainType.Text.ToString() + cb_TrainNum.Text.ToString() + "-" + cb_TrainRP.Text.ToString()+ de_Sntime.Text.Substring(0,4) + de_Sntime.Text.Substring(5, 2) + de_Sntime.Text.Substring(8,2) + de_Sntime.Text.Substring(11, 2) + de_Sntime.Text.Substring(14,2);
            }
            catch
            {
                tb_WorkCode.Text = "";
            }
        }
        #endregion
        private void Locomotive_Repair_Work_TrainIn_MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            if (cb_TrainClass.SelectedIndex > -1
                && cb_TrainType.SelectedIndex > -1
                && cb_TrainNum.SelectedIndex > -1
                && cb_TrainRP.SelectedIndex > -1
                && cb_TrainMW.SelectedIndex > -1
                && ((bt_Select_Map.Tag.ToString() !="-1" && rb_Type_1.Checked && tb_MapName.Text.Trim().Length > 0) || (rb_Type_0.Checked))
                && tb_WorkCode.Text.Trim().Length > 0
                && de_Intime.Text.Trim().Length > 0
                )
            {
                if (rb_Type_1.Checked)
                {
                    _MWID = cb_TrainMW.SelectedValue.ToString();
                    _TrainID = cb_TrainNum.SelectedValue.ToString();
                    _TTID = cb_TrainType.SelectedValue.ToString();
                    _TrainName = cb_TrainType.Text.ToString() + cb_TrainNum.Text.ToString();
                    //生成作业主流程数据
                    string strSQL = "insert into TS_TR_BU_Work_Main (f_type,f_state,f_mwid,f_workcode,f_rpid,f_trainid,f_tpmapid,f_tpmapname,f_tcid,f_tcname,f_ttid,f_ttname,f_name,f_stuffid,f_workerid,f_pitime,f_pstime,f_potime,f_petime,f_pltime,f_itime,f_stime) values(0,0,"
                        + _MWID + ",'"
                        + tb_WorkCode.Text.Trim() + "',"
                        + cb_TrainRP.SelectedValue.ToString() + ","
                        + cb_TrainNum.SelectedValue.ToString() + ","
                        + _MapID + ",'"
                        + _MapName + " ',"
                        + cb_TrainClass.SelectedValue.ToString() + ",'"
                        + cb_TrainClass.Text.ToString() + "',"
                        + cb_TrainType.SelectedValue.ToString() + ",'"
                        + cb_TrainType.Text.Trim() + "','"
                        + cb_TrainNum.Text.ToString() + "',"
                        + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ","
                        + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",'"
                        + de_Intime.Text.Trim() + "','"
                        + de_Sntime.Text.Trim() + "','"
                        + de_Ontime.Text.Trim() + "','"
                        + de_Entime.Text.Trim() + "','"
                        + de_Lntime.Text.Trim() + "','"
                        + de_Intime.Text.Trim() + "','"
                        + de_Sntime.Text.Trim() + "')";
                    _BMWID = TS_Win_Class.MySqlComGetID(strSQL);
                    strSQL = "insert into TS_TR_BU_Work_Main_Day(f_mwdid,f_state,f_bmwid,f_name,f_length,f_order,f_stuffid,f_workerid,f_type) select f_id as f_mwdid,0 as f_state," + _BMWID + " as f_bmwid,f_name,f_length,f_order," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_stuffid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid,f_type from TS_TR_PS_Work_Main_Day where f_del=0 and f_mwid=" + _MWID;
                    TS_Win_Class.MySqlCom(strSQL);
                    //生成顺序节点数据
                    strSQL = "insert into TS_TR_BU_Work_Main_Steps(f_type,f_timetype,f_pstime,f_petime,f_name,f_mwsid,f_bmwid,f_bmwdid,f_order,f_stuffid,f_workerid,f_length,f_state) select f_type,f_timetype,f_stime as f_pstime,f_etime as f_petime,f_name,f_id as f_mwsid," + _BMWID + ",(select f_id from TS_TR_BU_Work_Main_Day where f_mwdid=TS_TR_PS_Work_Main_Steps.f_mwdid and f_bmwid=" + _BMWID + ") as f_bmwdid,f_order," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_stuffid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid,f_length,-1 as f_state from TS_TR_PS_Work_Main_Steps where f_del=0 and f_mwid=" + _MWID + " and f_mwdid in (select f_id from TS_TR_PS_Work_Main_Day where f_mwid=" + _MWID + " and f_del=0)";
                    TS_Win_Class.MySqlCom(strSQL);
                    //生成作业组节点数据
                    strSQL = "insert into TS_TR_BU_Work_Main_Group(f_type,f_timetype,f_pstime,f_petime,f_name,f_mwgid,f_bmwid,f_bmwdid,f_bmwsid,f_order,f_must,f_length,f_pnum,f_typeid,f_typename,f_stuffid,f_workerid,f_state) select f_type,f_timetype,f_stime as f_pstime,f_etime as f_petime,f_name,f_id as f_mwgid," + _BMWID + ",(select f_id from TS_TR_BU_Work_Main_Day where f_mwdid=TS_TR_PS_Work_Main_Group.f_mwdid and f_bmwid=" + _BMWID + ") as f_bmwdid,(select f_id from TS_TR_BU_Work_Main_Steps where f_mwsid=TS_TR_PS_Work_Main_Group.f_mwsid and f_bmwid=" + _BMWID + ") as f_bmwsid,f_order,f_must,f_length,f_pnum,f_typeid,(select f_name from TS_S_T_P_WorkPlaceType where f_id=TS_TR_PS_Work_Main_Group.f_typeid) as f_typename," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_stuffid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid,-1 as f_state from TS_TR_PS_Work_Main_Group where f_del=0 and f_mwid=" + _MWID + " and f_mwdid in (select f_id from TS_TR_PS_Work_Main_Day where f_mwid=" + _MWID + " and f_del=0) and f_mwsid in (select f_id from TS_TR_PS_Work_Main_Steps where f_del=0 and f_mwid=" + _MWID + " and f_mwdid in (select f_id from TS_TR_PS_Work_Main_Day where f_mwid=" + _MWID + " and f_del=0))";
                    string _GroupNum = TS_Win_Class.MySqlComGetCount(strSQL);
                    System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(_CreatWorkPlace));
                    _pbf.iMaxValue = Convert.ToInt32(_GroupNum) + 3;
                    thread.Start();
                }
                if (rb_Type_0.Checked)
                {
                    _MWID = cb_TrainMW.SelectedValue.ToString();
                    _TrainID = cb_TrainNum.SelectedValue.ToString();
                    _TTID = cb_TrainType.SelectedValue.ToString();
                    _TrainName = cb_TrainType.Text.ToString() + cb_TrainNum.Text.ToString();
                    //生成作业主流程数据
                    string strSQL = "insert into TS_TR_BU_Work_Main (f_type,f_state,f_mwid,f_workcode,f_rpid,f_trainid,f_tpmapid,f_tpmapname,f_tcid,f_tcname,f_ttid,f_ttname,f_name,f_stuffid,f_workerid,f_pitime,f_pstime,f_potime,f_petime,f_pltime) values(0,-1,"
                        + _MWID + ",'"
                        + tb_WorkCode.Text.Trim() + "',"
                        + cb_TrainRP.SelectedValue.ToString() + ","
                        + cb_TrainNum.SelectedValue.ToString() + ","
                        + _MapID + ",'"
                        + _MapName + " ',"
                        + cb_TrainClass.SelectedValue.ToString() + ",'"
                        + cb_TrainClass.Text.ToString() + "',"
                        + cb_TrainType.SelectedValue.ToString() + ",'"
                        + cb_TrainType.Text.Trim() + "','"
                        + cb_TrainNum.Text.ToString() + "',"
                        + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ","
                        + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",'"
                        + de_Intime.Text.Trim() + "','"
                        + de_Sntime.Text.Trim() + "','"
                        + de_Ontime.Text.Trim() + "','"
                        + de_Entime.Text.Trim() + "','"
                        + de_Lntime.Text.Trim() + "')";
                    _BMWID = TS_Win_Class.MySqlComGetID(strSQL);
                    strSQL = "insert into TS_TR_BU_Work_Main_Day(f_mwdid,f_state,f_bmwid,f_name,f_length,f_order,f_stuffid,f_workerid,f_type) select f_id as f_mwdid,-1 as f_state," + _BMWID + " as f_bmwid,f_name,f_length,f_order," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_stuffid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid,f_type from TS_TR_PS_Work_Main_Day where f_del=0 and f_mwid=" + _MWID;
                    TS_Win_Class.MySqlCom(strSQL);
                    TS_Win_Class.Auto_Scheduling_Day(_BMWID,Convert.ToDateTime(de_Ontime.Text));
                    this.DialogResult = DialogResult.OK;
                }
                else
                {

                }
            }
            else
            {
                MessageBox.Show("数据不完整！");
            }
        }

        private void ShowProgressBar()
        {
            myHandle = new funHandle(_pbf.SetProgressValue);
            _pbf.ShowDialog();
        }
        private void _CreatWorkPlace()
        {
            MethodInvoker mi = new MethodInvoker(ShowProgressBar);
            this.BeginInvoke(mi);
            int i = 0;
            System.Threading.Thread.Sleep(10); // sleep to show window  
            string strSQL = "select f_id,f_bmwid,f_bmwdid,f_bmwsid,f_mwgid,f_name,(select count(*) from TS_TR_PS_Work_Main_Group_WorkPlace where f_mwgid=TS_TR_BU_Work_Main_Group.f_mwgid and f_del=0) as f_count from TS_TR_BU_Work_Main_Group where f_bmwid=" + _BMWID + " order by f_id asc";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                i++;
                _pbf.sName = read["f_name"].ToString() + "添加项点数据" + read["f_count"].ToString() + "条";
                strSQL = "insert into TS_TR_BU_Work_Main_Group_WorkPlace(f_name,f_mwwpid,f_bmwid,f_bmwdid,f_bmwsid,f_bmwgid,f_wpid,f_brief,f_tdid,f_tdname,f_type,f_typeid,f_typename,f_order,f_must,f_mustname,f_photo,f_rank,f_rankname,f_dtid,f_dtname,f_drid,f_drname,f_drsign,f_dataunit,f_data1,f_data2,f_stuffid,f_workerid,f_state) select f_name,f_id as f_mwwpid," + _BMWID + " as f_bmwid,(select f_id from TS_TR_BU_Work_Main_Day where f_mwdid=TS_TR_PS_Work_Main_Group_WorkPlace.f_mwdid and f_bmwid=" + _BMWID + ") as f_bmwdid,(select f_id from TS_TR_BU_Work_Main_Steps where f_mwsid=TS_TR_PS_Work_Main_Group_WorkPlace.f_mwsid and f_bmwid=" + _BMWID + ") as f_bmwsid,(select f_id from TS_TR_BU_Work_Main_Group where f_mwgid=TS_TR_PS_Work_Main_Group_WorkPlace.f_mwgid and f_bmwid=" + _BMWID + ") as f_bmwgid,f_wpid,f_brief,f_tdid,(select f_name from TS_S_T_P_TrainDepSys where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_tdid)as f_tdname,f_type,f_typeid,(select f_name from TS_S_T_P_WorkPlaceType where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_typeid)as f_typename,f_order,f_must,(select f_name from TS_S_T_B_TrainMust where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_must)as f_mustname,f_photo,f_rank,(select f_name from TS_S_T_B_TrainRank where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_rank)as f_rankname,f_dtid,(select f_name from TS_S_T_B_TrainDataType where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_dtid)as f_dtname,f_drid,f_drname,(select f_sign from TS_S_T_B_TrainDataResult where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_drid)as f_drsign,f_dataunit,f_data1,f_data2," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_stuffid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid,-1 as f_state from TS_TR_PS_Work_Main_Group_WorkPlace where f_mwgid =" + read["f_mwgid"].ToString() + " and f_del=0";
                //strSQL = "insert into TS_TR_BU_Work_Main_Group_WorkPlace(f_name,f_mwwpid,f_bmwid,f_bmwdid,f_bmwsid,f_bmwgid,f_wpid,f_brief,f_tdid,f_tdname,f_type,f_typeid,f_typename,f_perfid,f_perfname,f_rull,f_must,f_mustname,f_photo,f_rank,f_rankname,f_dtid,f_dtname,f_drid,f_drname,f_drsign,f_dataunit,f_data1,f_data2,f_stuffid,f_workerid) select f_name,f_id as f_mwwpid," + _BMWID + " as f_bmwid," + read["f_bmwdid"].ToString() + " as f_bmwdid," + read["f_bmwsid"].ToString() + " as f_bmwsid," + read["f_id"].ToString() + " as f_bmwgid,f_wpid,f_brief,f_tdid,(select f_name from TS_S_T_P_TrainDepSys where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_tdid)as f_tdname,f_type,f_typeid,(select f_name from TS_S_T_P_WorkPlaceType where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_typeid)as f_typename,f_rull,f_must,(select f_name from TS_S_T_B_TrainMust where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_must)as f_mustname,f_photo,f_rank,(select f_name from TS_S_T_B_TrainRank where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_rank)as f_rankname,f_dtid,(select f_name from TS_S_T_B_TrainDataType where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_dtid)as f_dtname,f_drid,f_drname,(select f_sign from TS_S_T_B_TrainDataResult where f_id=TS_TR_PS_Work_Main_Group_WorkPlace.f_drid)as f_drsign,f_dataunit,f_data1,f_data2," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_stuffid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid from TS_TR_PS_Work_Main_Group_WorkPlace where f_mwgid =" + read["f_mwgid"].ToString() + " and f_del=0";
                TS_Win_Class.MySqlCom(strSQL);
                System.Threading.Thread.Sleep(10);
                // 这里直接调用代理  
                this.Invoke(this.myHandle, new object[] { (i) });
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            i++;
            //占用工位
            i++;
            _pbf.sName = "更新工位信息数据";
            strSQL = "update TS_S_B_Station_WorkPlace set f_stacknum=f_stacknum+1 where f_id=" + _MapID;
            TS_Win_Class.MySqlCom(strSQL);
            System.Threading.Thread.Sleep(100);
            this.Invoke(this.myHandle, new object[] { (i) });
            i++;
            _pbf.sName = "更新作业流程时间节点数据";
            TS_Win_Class.Auto_Scheduling_Day(_BMWID, Convert.ToDateTime(de_Ontime.Text));
            //TS_Win_Class.Auto_Scheduling_Steps(_BMWID);
            //TS_Win_Class.Auto_Scheduling_Group(_BMWID);
            System.Threading.Thread.Sleep(100);
            this.Invoke(this.myHandle, new object[] { (i) });
            this.DialogResult = DialogResult.OK;
        }
        private void _AutoSumWarning()
        {
            if (TS_Win_Class.GetSQlFieldValue("select f_arsid from TS_S_T_P_Train_List where f_id=" + _TrainID, "f_arsid") == TS_Win_Locomotive_MainForm.pUserInfoMain.UserRSID)
            {
                string strSQL = "select * from TS_TR_PS_Work_Warning where f_mwid=" + _MWID + " and f_ttid=" + _TTID + " and (getdate() between f_stime and f_etime) and f_del=0";
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand comm = new SqlCommand(strSQL, conn);
                SqlDataReader read = comm.ExecuteReader();
                SqlConnection conn_s = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn_s.Open();
                SqlCommand comm_s = new SqlCommand("select getdate()", conn_s);
                SqlDataReader read_s = comm_s.ExecuteReader();
                while (read.Read())
                {
                    read_s.Close();
                    strSQL = "select *,(select f_name from TS_S_T_P_TrainTC_WorkPlace where f_id=TS_TR_PS_Work_Warning_Place.f_wpid) as f_name from TS_TR_PS_Work_Warning_Place where f_wwid=" + read["f_id"].ToString() + " and f_del=0";
                    comm_s = new SqlCommand(strSQL, conn_s);
                    read_s = comm_s.ExecuteReader();
                    Boolean _bl = false;
                    while (read_s.Read())
                    {
                        _bl = false;
                        switch (read["f_rtype"].ToString())
                        {
                            case "0"://单次
                                //先判断其他整备点是否处置过
                                strSQL = "select count(*) as f_count from TS_TR_BU_Work_Main_Warning where (f_time between '" + read["f_stime"].ToString() + "' and '" + read["f_etime"].ToString() + "') and f_wpid=" + read_s["f_wpid"].ToString() + " and f_trainid=" + _TrainID;
                                //交换库内是否处置过
                                if (TS_Win_Class.GetSQlFieldValue(strSQL, "f_count") == "0")
                                {
                                    //判断本项目时间段内是否处置过
                                    strSQL = "select count(*) as f_count from TS_TR_BU_Work_Main_Group_WorkPlace where (f_time between '" + read["f_stime"].ToString() + "' and '" + read["f_etime"].ToString() + "') and f_wpid=" + read_s["f_wpid"].ToString() + " and f_bmwid in (select f_id from TS_TR_BU_Work_Main where f_id<>" + _BMWID + " and f_mwid=" + _MWID + " and f_trainid=" + _TrainID + " and f_del=0 and (f_stime between '" + read["f_stime"].ToString() + "' and '" + read["f_etime"].ToString() + "') )";
                                    if (TS_Win_Class.GetSQlFieldValue(strSQL, "f_count") == "0")
                                    {
                                        _bl = true;
                                    }
                                    else
                                    {
                                        _bl = false;
                                    }
                                }
                                else
                                {
                                    _bl = false;
                                }
                                break;
                            case "1"://每次
                                _bl = true;
                                break;
                            case "2"://循环
                                Boolean _bl_i; Boolean _bl_o = false;//是否有数据
                                int _dt_i = 0; int _dt_o = 0;//最后作业时间间隔时间
                                //本库
                                strSQL = "select count(*) as f_count from TS_TR_BU_Work_Main_Group_WorkPlace where (f_time between '" + read["f_stime"].ToString() + "' and '" + read["f_etime"].ToString() + "') and f_wpid=" + read_s["f_wpid"].ToString() + " and f_bmwid in (select f_id from TS_TR_BU_Work_Main where f_id<>" + _BMWID + " and f_mwid=" + _MWID + " and f_trainid=" + _TrainID + " and f_del=0 and (f_stime between '" + read["f_stime"].ToString() + "' and '" + read["f_etime"].ToString() + "') ) and f_del=0";
                                if (TS_Win_Class.GetSQlFieldValue(strSQL, "f_count") == "0")
                                {
                                    _bl_i = false;
                                }
                                else
                                {
                                    _bl_i = true;
                                    strSQL = "select top 1 datediff(day,f_time,getdate()) as f_day from TS_TR_BU_Work_Main_Group_WorkPlace where (f_time between '" + read["f_stime"].ToString() + "' and '" + read["f_etime"].ToString() + "') and f_wpid=" + read_s["f_wpid"].ToString() + " and f_bmwid in (select f_id from TS_TR_BU_Work_Main where f_id<>" + _BMWID + " and f_mwid=" + _MWID + " and f_trainid=" + _TrainID + " and f_del=0 and (f_stime between '" + read["f_stime"].ToString() + "' and '" + read["f_etime"].ToString() + "') ) and f_del=0 order by f_time desc";
                                    _dt_i = Convert.ToInt16(TS_Win_Class.GetSQlFieldValue(strSQL, "f_day"));
                                }
                                //交换库
                                strSQL = "select count(*) as f_count from TS_TR_BU_Work_Main_Warning where (f_time between '" + read["f_stime"].ToString() + "' and '" + read["f_etime"].ToString() + "') and f_wpid=" + read_s["f_wpid"].ToString() + " and f_trainid=" + _TrainID;
                                if (TS_Win_Class.GetSQlFieldValue(strSQL, "f_count") == "0")
                                {
                                    _bl_o = false;
                                }
                                else
                                {
                                    _bl_o = true;
                                    strSQL = "select top 1 datediff(day,f_time,getdate()) as f_day from TS_TR_BU_Work_Main_Warning where (f_time between '" + read["f_stime"].ToString() + "' and '" + read["f_etime"].ToString() + "') and f_wpid=" + read_s["f_wpid"].ToString() + " and f_trainid=" + _TrainID + " order by f_time desc";
                                    _dt_o = Convert.ToInt16(TS_Win_Class.GetSQlFieldValue(strSQL, "f_day"));
                                }
                                if (_bl_i && _bl_o)
                                {
                                    if (_dt_i < _dt_o)
                                    {
                                        if (_dt_i >= (int)read["f_loop"] - (int)read["f_ahead"])
                                        {
                                            _bl = true;
                                        }
                                    }
                                    else
                                    {
                                        if (_dt_o >= (int)read["f_loop"] - (int)read["f_ahead"])
                                        {
                                            _bl = true;
                                        }
                                    }
                                }
                                else if (!_bl_i && !_bl_o)
                                {
                                    _bl = false;
                                }
                                else
                                {
                                    if (_bl_i)
                                    {
                                        if (_dt_i >= (int)read["f_loop"] - (int)read["f_ahead"])
                                        {
                                            _bl = true;
                                        }
                                    }
                                    if (_bl_o)
                                    {
                                        if (_dt_o >= (int)read["f_loop"] - (int)read["f_ahead"])
                                        {
                                            _bl = true;
                                        }
                                    }
                                }
                                break;
                        }
                        if (_bl)//该项点需要作业
                        {
                            //项点加入整备流程
                            strSQL = "insert into TS_TR_BU_Work_Main_Group_WorkPlace(f_name,f_mwwpid,f_bmwid,f_bmwdid,f_bmwsid,f_bmwgid,f_wpid,f_brief,f_tdid,f_tdname,f_typeid,f_typename,f_perfid,f_perfname,f_rull,f_must,f_mustname,f_photo,f_rank,f_rankname,f_dtid,f_dtname,f_drid,f_drname,f_drsign,f_dataunit,f_data1,f_data2,f_stuffid,f_workerid) select f_name,-1 as f_mwwpid," + _BMWID + " as f_bmwid,(select f_bmwdid from TS_TR_BU_Work_Main_Group where f_mwgid=" + read_s["f_mwgid"].ToString() + " and f_bmwid=" + _BMWID + ") as f_bmwdid,(select f_bmwsid from TS_TR_BU_Work_Main_Group where f_mwgid=" + read_s["f_mwgid"].ToString() + " and f_bmwid=" + _BMWID + ") as f_bmwsid,(select f_id from TS_TR_BU_Work_Main_Group where f_mwgid=" + read_s["f_mwgid"].ToString() + " and f_bmwid=" + _BMWID + ") as f_bmwgid,f_id as f_wpid,f_brief,f_tdid,(select f_name from TS_S_T_P_TrainDepSys where f_id=TS_S_T_P_TrainTC_WorkPlace.f_tdid)as f_tdname,f_typeid,(select f_name from TS_S_T_P_WorkPlaceType where f_id=TS_S_T_P_TrainTC_WorkPlace.f_typeid)as f_typename,f_perfid,(select f_name from TS_S_T_P_TrainPerf where f_id=TS_S_T_P_TrainTC_WorkPlace.f_perfid) as f_perfname,f_rull,f_must,(select f_name from TS_S_T_B_TrainMust where f_id=TS_S_T_P_TrainTC_WorkPlace.f_must)as f_mustname,f_photo,f_rank,(select f_name from TS_S_T_B_TrainRank where f_id=TS_S_T_P_TrainTC_WorkPlace.f_rank)as f_rankname,f_dtid,(select f_name from TS_S_T_B_TrainDataType where f_id=TS_S_T_P_TrainTC_WorkPlace.f_dtid)as f_dtname,f_drid,f_drname,(select f_sign from TS_S_T_B_TrainDataResult where f_id=TS_S_T_P_TrainTC_WorkPlace.f_drid)as f_drsign,f_dataunit,f_data1,f_data2," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_stuffid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid from TS_S_T_P_TrainTC_WorkPlace where f_id =" + read_s["f_wpid"].ToString() + " and f_del=0";
                            string _BMWWPID = TS_Win_Class.MySqlComGetID(strSQL);
                            //找到BMWID，BMWDID，BMWSID，BMWGID，
                            //加入整备流程
                            if (read_s["f_jt6"].ToString() == "1")//需要自动报JT6
                            {
                                string strVoice = _TrainName + " 有一个预警普查活件产生";
                                string strType = "";
                                string strRID = "";
                                strSQL = "insert into TS_TR_TM_Work_Main_JT6(f_bmwid,f_ttname,f_name,f_workcode,f_bmwdid,f_bmwdname,f_bmwsid,f_bmwsname,f_bmwgid,f_bmwgname,f_bmwwpid,f_bmwwpname,f_maintype,f_mainid,f_stuffid,f_brief) select f_bmwid,(select f_ttname from TS_TR_BU_Work_Main where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwid)as f_ttname,(select f_name from TS_TR_BU_Work_Main where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwid)as f_name,(select f_workcode from TS_TR_BU_Work_Main where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwid)as f_workcode,f_bmwdid,(select f_name from TS_TR_BU_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwdid)as f_bmwdname,f_bmwsid,(select f_name from TS_TR_BU_Work_Main_Steps where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwsid)as f_bmwsname,f_bmwgid,(select f_name from TS_TR_BU_Work_Main_Group where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwgid)as f_bmwgname,f_id as f_bmwwpid,f_name as f_bmwwpname,4 as f_maintype,f_id as f_mainid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_stuffid,'预警普查' as f_brief from TS_TR_BU_Work_Main_Group_WorkPlace where f_id=" + _BMWWPID;
                                TS_Win_Class.MySqlCom(strSQL);
                                strType = "4";
                                strRID = _BMWWPID;
                                strVoice += ",活件产生于 " + read_s["f_name"].ToString();
                                strVoice += ",故障现象为 系统自动报活,报活人 " + TS_Win_Locomotive_MainForm.pUserInfoMain.Username;
                                strSQL = "insert into TS_TR_BU_Work_Main_Relation(f_rtype,f_rid,f_ftype,f_fid,f_workerid) select " + strType + " as f_rtype," + strRID + " as f_rid,0 as f_ftype,f_id as f_fid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid from TS_S_T_P_Stuff where f_id in (select f_stuffid from TS_S_TR_Rolse_Stuff where f_del=0 and f_srid=1) and f_depid=(select top 1 f_depid from TS_S_T_S_NowWorkDep where f_del=0)";
                                //                            strSQL = "insert into TS_TR_BU_Work_Main_Relation(f_rtype,f_rid,f_ftype,f_fid,f_workerid) select " + strType + " as f_rtype," + strRID + " as f_rid,0 as f_ftype,f_id as f_fid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid from TS_S_T_P_Stuff where f_id in (select f_stuffid from TS_S_TR_Rolse_Stuff where f_del=0 and f_srid=1) and f_depid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex;
                                string _ID = TS_Win_Class.MySqlComGetID(strSQL);
                                if (_ID == "")
                                {
                                    MessageBox.Show("单前作业班组内未指定作业工长角色，报活有效但是未指定活件确认人！");
                                }
                                else
                                {
                                    strSQL = "insert into TS_S_T_P_Voice_Announce(f_vid,f_tablename,f_string,f_funcid,f_type,f_workerid) select " + _ID + " as f_vid,'TS_TR_BU_Work_Main_Relation' as f_tablename,'" + strVoice + "' as f_string,f_id as f_funcid,0 as f_type," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid from TS_S_T_P_Stuff TS_S_T_P_Stuff where f_id in (select f_stuffid from TS_S_TR_Rolse_Stuff where f_del=0 and f_srid=1) and f_depid=(select top 1 f_depid from TS_S_T_S_NowWorkDep where f_del=0)";
                                    //                                strSQL = "insert into TS_S_T_P_Voice_Announce(f_vid,f_tablename,f_string,f_funcid,f_type,f_workerid) select " + _ID + " as f_vid,'TS_TR_BU_Work_Main_Relation' as f_tablename,'" + strVoice + "' as f_string,f_id as f_funcid,0 as f_type," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid from TS_S_T_P_Stuff TS_S_T_P_Stuff where f_id in (select f_stuffid from TS_S_TR_Rolse_Stuff where f_del=0 and f_srid=1) and f_depid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex;
                                    TS_Win_Class.MySqlCom(strSQL);
                                }
                            }
                        }
                    }
                }
                read.Close();
                conn.Close();
                conn.Dispose();
                read_s.Close();
                conn_s.Close();
                conn_s.Dispose();
            }
        }
        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void cb_TrainClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_TrainClass.SelectedIndex > -1)
            {
                _Initcb_TrainRP();
                _Initcb_TrainType();
                if (cb_TrainType.SelectedIndex > -1)
                {
                    //未加入匹配的扣车计划和库内机车的排异
                    _Initcb_TrainNum();
                }
                if (cb_TrainRP.SelectedIndex > -1 && cb_TrainClass.SelectedIndex > -1 && cb_TrainType.SelectedIndex > -1)
                {
                    _Initcb_TrainMW();
                }
                else
                {
                    cb_TrainMW.DataSource = null;
                    cb_TrainMW.Items.Clear();
                }
            }
            _CreatWorkerCode();
        }
        private void cb_TrainType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_TrainType.SelectedIndex > -1)
            {
                //未加入匹配的扣车计划和库内机车的排异
                _Initcb_TrainNum();
                bt_Add.Enabled = true;
            }
            else
            {
                bt_Add.Enabled = false;
            }
            if (cb_TrainRP.SelectedIndex > -1 && cb_TrainClass.SelectedIndex > -1 && cb_TrainType.SelectedIndex > -1)
            {
                _Initcb_TrainMW();
            }
            else
            {
                cb_TrainMW.DataSource = null;
                cb_TrainMW.Items.Clear();
            }
            _CreatWorkerCode();
        }

        private void cb_TrainRP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_TrainRP.SelectedIndex > -1 && cb_TrainClass.SelectedIndex > -1 && cb_TrainType.SelectedIndex > -1)
            {
                _Initcb_TrainMW();
            }
            else
            {
                cb_TrainMW.DataSource = null;
                cb_TrainMW.Items.Clear();
            }
            _CreatWorkerCode();
        }

        private void cb_TrainNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            _CreatWorkerCode();
        }
        #region//--时间控件--//
        private void de_Intime_EditValueChanged(object sender, EventArgs e)
        {
            de_Sntime.EditValue = ((DateTime)de_Intime.EditValue).AddDays(1);
        }
        private void de_Sntime_EditValueChanged(object sender, EventArgs e)
        {
            de_Ontime.EditValue = ((DateTime)de_Sntime.EditValue).AddDays(1);
        }
        private void de_Ontime_EditValueChanged(object sender, EventArgs e)
        {
            de_Entime.EditValue = ((DateTime)de_Ontime.EditValue).AddDays(Convert.ToDouble(TS_Win_Class.GetSQlFieldValue("select sum(f_length)-1 as f_sum from TS_TR_PS_Work_Main_Day where f_mwid="+cb_TrainMW.SelectedValue.ToString()+" and f_del=0", "f_sum")));
            de_Lntime.EditValue = ((DateTime)de_Entime.EditValue).AddDays(1);
            _CreatWorkerCode();
        }
        #endregion

        private void bt_ShowMap_Click(object sender, EventArgs e)
        {
            //if (cb_TrainMW.SelectedIndex > -1)
            //{
            //    //MessageBox.Show(cb_TrainMW.SelectedValue.ToString() + cb_TrainMW.SelectedText.ToString());
            //    Pub.MFD.Locomotive.Train.Pub_MDF_Locomotive_Train_Main_Show_Form Pub_MDF_Locomotive_Train_Main_Show_Form = new Pub.MFD.Locomotive.Train.Pub_MDF_Locomotive_Train_Main_Show_Form();
            //    Pub_MDF_Locomotive_Train_Main_Show_Form._strLoadID = "T" + cb_TrainMW.SelectedValue.ToString();
            //    Pub_MDF_Locomotive_Train_Main_Show_Form._strLoadName = cb_TrainMW.Text.ToString();
            //    Pub_MDF_Locomotive_Train_Main_Show_Form._strLoadType = "_MDF_TR_Show";
            //    Pub_MDF_Locomotive_Train_Main_Show_Form.ShowDialog();
            //}
        }

        private void bt_Select_Map_Click(object sender, EventArgs e)
        {
            uc_Pub_Map_Select_ListView_ByOne uc_Pub_Map_Select_ListView_ByOne = new uc_Pub_Map_Select_ListView_ByOne();
            uc_Pub_Map_Select_ListView_ByOne._LoadType = "0";
            if (uc_Pub_Map_Select_ListView_ByOne.ShowDialog() == DialogResult.OK)
            {
                GetMapValue(_MapName, _MapID);
            }
            else
            {
                GetMapValue("", "-1");
            }
        }
        public void GetMapValue(string _Name, string _ID)
        {
            bt_Select_Map.Tag = _ID;
            tb_MapName.Text = _Name;
        }

        private void cb_TrainMW_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_TrainMW.SelectedIndex>-1)
            {
                gb_Time.Enabled = true;
            }
            else
            {
                gb_Time.Enabled = false;
            }
            _CreatWorkerCode();
        }

        private void bt_Add_Click(object sender, EventArgs e)
        {
            uc_Locomotive_MainBase_TrainList_AddNewNum_Simple _uc = new uc_Locomotive_MainBase_TrainList_AddNewNum_Simple();
            _uc._TCID = cb_TrainClass.SelectedValue.ToString();
            _uc._TCName = cb_TrainClass.SelectedText.ToString();
            _uc._TTID = cb_TrainType.SelectedValue.ToString();
            _uc._TTName = cb_TrainType.Text.ToString();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                _Initcb_TrainNum();
                if (cb_TrainRP.SelectedIndex > -1 && cb_TrainClass.SelectedIndex > -1 && cb_TrainType.SelectedIndex > -1)
                {
                    _Initcb_TrainMW();
                }
                _CreatWorkerCode();
            }
            else
            {
            }
        }

    }
}